using System;
using System.Data;
using System.Windows.Forms;
using BLL;
using Common;
using Model;

namespace UI {
    public partial class FrmAddMember : Form {
        private MemberBLL _memberBll = MemberBLL.GetMemberBll();
        public FrmAddMember() {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void btnOk_Click(object sender, EventArgs e) {
            Result<Member> result = _memberBll.AddMember(new Member() {
                Mname = txtMname.Text,
                Gender = rdoMan.Checked ? "男" : "女",
                Type = int.Parse(cmbMemType.SelectedValue.ToString()),
                Discount = double.Parse(txtDisount.Text),
                Money = double.Parse(txtMoney.Text),
                Points = int.Parse(txtPoints.Text),
                Birthday = birDate.Value,
                PhoneNum = txtPhoneNum.Text,
                RegistDate = registDate.Value,
                EndDate = endDate.Value
            });
            
            MessageBox.Show(result.Message);
            if (result.IsSuccess) {
                foreach (Control control in this.Controls) {
                    if (control is TextBox textBox) {
                        textBox.Clear();
                    }
                }
            }
        }

        private void FrmAddMember_Load(object sender, EventArgs e) {
            //初始化combox控件
            DataTable dataTable = _memberBll.QueryMemberType();
            cmbMemType.DisplayMember = "tname";
            cmbMemType.ValueMember = "tid";
            cmbMemType.DataSource = dataTable;
        }

        private void FrmAddMember_FormClosing(object sender, FormClosingEventArgs e) {
            FrmMemberOperation.ObjFrmAddMember = null;
        }
    }
}